﻿import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
Vue.use(VueRouter)

// 生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// 全局路由(无需权限要求)
const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: _import('page/login/index'),
    meta: {
      requireAuth: false
    }
  },
  {
    path: '/signIn',
    name: 'signIn',
    component: _import('page/signIn/index'),
    meta: {
      requireAuth: false
    }
  },
  {
    path: '/',
    component: _import('layout/index'),
    redirect: '/businessOverview/workbench',
    meta: {
      requireAuth: true
    },
    children: [
      {
        path: '/403',
        name: '403',
        component: _import('page/exception/exception403/index'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/404',
        name: '404',
        component: _import('page/exception/exception404/index'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/businessOverview',
        name: 'businessOverview',
        component: _import('page/businessOverview/index'),
        meta: {
          requireAuth: true,
          key: 'businessOverview',
          icon: 'home',
          title: '业务总览'
        },
        children: [
          {
            path: '/businessOverview/workbench',
            name: 'workbench',
            component: _import('page/businessOverview/workbench/index'),
            meta: {
              requireAuth: true,
              key: 'workbench',
              title: '工作台'
            }
          },
          {
            path: '/businessOverview/dataMonitor',
            name: 'dataMonitor',
            component: _import('page/businessOverview/dataMonitor/index'),
            meta: {
              requireAuth: true,
              key: 'dataMonitor',
              title: '数据监控'
            }
          }
        ]
      },
      {
        path: '/emergencyControl',
        name: 'emergencyControl',
        component: _import('page/emergencyControl/index'),
        meta: {
          requireAuth: true,
          key: 'emergencyControl',
          icon: 'container',
          title: '应急管控方案'
        },
        children: [
          {
            path: '/emergencyControl/schemeManagement',
            name: 'schemeManagement',
            component: _import('page/emergencyControl/schemeManagement/index'),
            meta: {
              requireAuth: true,
              key: 'schemeManagement',
              title: '方案管理'
            }
          },
          {
            path: '/emergencyControl/schemeStart',
            name: 'schemeStart',
            component: _import('page/emergencyControl/schemeStart/index'),
            meta: {
              requireAuth: true,
              key: 'schemeStart',
              title: '方案启动'
            }
          },
          {
            path: '/emergencyControl/executionAnalysis',
            name: 'executionAnalysis',
            component: _import('page/emergencyControl/executionAnalysis/index'),
            meta: {
              requireAuth: true,
              key: 'executionAnalysis',
              title: '执行分析'
            }
          }
        ]
      },
      {
        path: '/energyEfficiency',
        name: 'energyEfficiency',
        component: _import('page/energyEfficiency/index'),
        meta: {
          requireAuth: true,
          key: 'energyEfficiency',
          icon: 'line-chart',
          title: '能效监测'
        },
        children: [
          {
            path: '/energyEfficiency/equipmentMonitoring',
            name: 'equipmentMonitoring',
            component: _import('page/energyEfficiency/equipmentMonitoring/index'),
            meta: {
              requireAuth: true,
              key: 'equipmentMonitoring',
              title: '设备监测'
            }
          },
          {
            path: '/energyEfficiency/exceptionDetails',
            name: 'exceptionDetails',
            component: _import('page/energyEfficiency/exceptionDetails/index'),
            meta: {
              requireAuth: true,
              key: 'exceptionDetails',
              title: '异常明细'
            }
          },
          {
            path: '/energyEfficiency/enterpriseManagement',
            name: 'enterpriseManagement',
            component: _import('page/energyEfficiency/enterpriseManagement/index'),
            meta: {
              requireAuth: true,
              key: 'enterpriseManagement',
              title: '企业名单管理'
            }
          }
        ]
      },
      {
        path: '/statisticAnalysis',
        name: 'statisticAnalysis',
        component: _import('page/statisticAnalysis/index'),
        meta: {
          requireAuth: true,
          key: 'statisticAnalysis',
          icon: 'pie-chart',
          title: '统计分析'
        }
      },
      {
        path: '/enterpriseInformationManagement',
        name: 'enterpriseInformationManagement',
        component: _import('page/enterpriseInformationManagement/index'),
        meta: {
          requireAuth: true,
          key: 'enterpriseInformationManagement',
          icon: 'project',
          title: '企业基础信息管理'
        },
        children: [
          {
            path: '/enterpriseInformationManagement/enterpriseInformation',
            name: 'enterpriseInformation',
            component: _import('page/enterpriseInformationManagement/enterpriseInformation/index'),
            meta: {
              requireAuth: true,
              key: 'enterpriseInformation',
              title: '企业基础信息'
            }
          },
          {
            path: '/enterpriseInformationManagement/enterpriseInformation/new',
            name: 'enterpriseInformationCreate',
            component: _import('page/enterpriseInformationManagement/enterpriseInformation/create'),
            meta: {
              requireAuth: true,
              key: 'enterpriseInformationCreate',
              title: '新增企业基础信息'
            }
          },
          {
            path: '/enterpriseInformationManagement/enterpriseInformation/edit',
            name: 'enterpriseInformationEdit',
            component: _import('page/enterpriseInformationManagement/enterpriseInformation/edit'),
            meta: {
              requireAuth: true,
              key: 'enterpriseInformationEdit',
              title: '编辑企业基础信息'
            }
          },
          {
            path: '/enterpriseInformationManagement/enterpriseInformation/view',
            name: 'enterpriseInformationView',
            component: _import('page/enterpriseInformationManagement/enterpriseInformation/view'),
            meta: {
              requireAuth: true,
              key: 'enterpriseInformationView',
              title: '查看企业基础信息'
            }
          },
          {
            path: '/enterpriseInformationManagement/equipmentManagement',
            name: 'equipmentManagement',
            component: _import('page/enterpriseInformationManagement/equipmentManagement/index'),
            meta: {
              requireAuth: true,
              key: 'equipmentManagement',
              title: '设备管理'
            }
          }
        ]
      },
      {
        path: '/ruleConfig',
        name: 'ruleConfig',
        component: _import('page/ruleConfig/index'),
        meta: {
          requireAuth: true,
          key: 'ruleConfig',
          icon: 'audit',
          title: '规则配置'
        },
        children: [
          {
            path: '/ruleConfig/generalRules',
            name: 'generalRules',
            component: _import('page/ruleConfig/generalRules/index'),
            meta: {
              requireAuth: true,
              key: 'generalRules',
              title: '通用规则'
            }
          },
          {
            path: '/ruleConfig/customRule',
            name: 'customRule',
            component: _import('page/ruleConfig/customRule/index'),
            meta: {
              requireAuth: true,
              key: 'customRule',
              title: '自定义规则'
            }
          },
          {
            path: '/ruleConfig/oneFactoryOnePolicy',
            name: 'oneFactoryOnePolicy',
            component: _import('page/ruleConfig/oneFactoryOnePolicy/index'),
            meta: {
              requireAuth: true,
              key: 'oneFactoryOnePolicy',
              title: '一厂一策规则'
            }
          }
        ]
      },
      {
        path: '/mobileManagement',
        name: 'mobileManagement',
        component: _import('page/mobileManagement/index'),
        meta: {
          requireAuth: true,
          key: 'mobileManagement',
          icon: 'mobile',
          title: '移动端管理'
        }
      },
      {
        path: '/permissionManagement',
        name: 'permissionManagement',
        component: _import('page/permissionManagement/index'),
        meta: {
          requireAuth: true,
          key: 'permissionManagement',
          icon: 'key',
          title: '权限管理'
        },
        children: [
          {
            path: '/permissionManagement/organization',
            name: 'organization',
            component: _import('page/permissionManagement/organization/index'),
            meta: {
              requireAuth: true,
              key: 'organization',
              title: '行政区划'
            }
          },
          {
            path: '/permissionManagement/roleManagement',
            name: 'roleManagement',
            component: _import('page/permissionManagement/roleManagement/index'),
            meta: {
              requireAuth: true,
              key: 'roleManagement',
              title: '角色管理'
            }
          },
          {
            path: '/permissionManagement/userManagement',
            name: 'userManagement',
            component: _import('page/permissionManagement/userManagement/index'),
            meta: {
              requireAuth: true,
              key: 'userManagement',
              title: '用户管理'
            }
          },
          {
            path: '/permissionManagement/menuManagement',
            name: 'menuManagement',
            component: _import('page/permissionManagement/menuManagement/index'),
            meta: {
              requireAuth: true,
              key: 'menuManagement',
              title: '菜单管理'
            }
          }
        ]
      },
      {
        path: '/messageCenter',
        name: 'messageCenter',
        component: _import('page/messageCenter/index'),
        meta: {
          requireAuth: true,
          key: 'messageCenter',
          icon: 'bell',
          title: '消息中心'
        }
      },
      {
        path: '/systemManagement',
        name: 'systemManagement',
        component: _import('page/systemManagement/index'),
        meta: {
          requireAuth: true,
          key: 'systemManagement',
          icon: 'setting',
          title: '系统管理'
        },
        children: [
          {
            path: '/systemManagement/editPassword',
            name: 'editPassword',
            component: _import('page/systemManagement/editPassword/index'),
            meta: {
              requireAuth: true,
              key: 'editPassword',
              title: '修改密码'
            }
          }
        ]
      }
    ]
  },
  {
    path: '*', redirect: '/404', hidden: true
  }
]

// 异步路由(需要获取对应权限)
// const asyncRoutes = [
// {
//   path: '/',
//   component: _import('layout/index'),
//   redirect: '/businessOverview/workbench',
//   meta: {
//     requireAuth: true
//   },
//   children: [

//   ]
// }
// ]

// 创建路由
const createRouter = () => {
  return new VueRouter({
    mode: 'hash',
    routes: constantRoutes
  })
}

const router = createRouter()

// 重置路由
const resetRouter = function () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

// 路由拦截
router.beforeEach(async (to, from, next) => {
  // 是否登录
  if (store.state.user.token) {
    next()
  } else {
    // 用户没登录，跳转到登录页面
    if (to.path === '/signIn') {
      next()
    } else {
      next('/signIn')
    }
  }
})

export { constantRoutes, resetRouter }
export default router
